function multiDraggable_do(jqueryDiv) {
    multiDraggable_arrayState = {};
    multiDraggable_arraySelectedNode = {};

    jqueryDiv.mousedown(multiDraggable_mouseDown);
    $(document).mouseup(multiDraggable_mouseUp);
    $(document).mousemove(multiDraggable_mouseMove);
    $(document).keydown(multiDraggable_keyDown);
    $(document).keyup(multiDraggable_keyUp);
}


function multiDraggable_mouseDown(event) {
    multiDraggable_arrayState["mouseX"] = event.pageX;
    multiDraggable_arrayState["mouseY"] = event.pageY;
    multiDraggable_arrayState["mousePressed"] = true;
    multiDraggable_arrayState["mouseMoved"] = false;
    multiDraggable_arrayState["nodeWhereMouseDown"] = $(this);
    multiDraggable_arrayState["nodeWhereMouseDownWasSelected"] = true;

    if (!multiDraggable_arrayState["controlKeyPressed"]) {
        for (var nodeId in multiDraggable_arraySelectedNode) {
            if (multiDraggable_arraySelectedNode[nodeId] == undefined)
                continue;

            $("#" + nodeId).css("border-color", "white");
        }

        multiDraggable_arraySelectedNode = {};
    }

    if (multiDraggable_arraySelectedNode[multiDraggable_arrayState["nodeWhereMouseDown"].attr("id")] == undefined) {
        multiDraggable_arraySelectedNode[multiDraggable_arrayState["nodeWhereMouseDown"].attr("id")] = true;
        multiDraggable_arrayState["nodeWhereMouseDownWasSelected"] = false;
        multiDraggable_arrayState["nodeWhereMouseDown"].css("border-color", "red");
    }
}


function multiDraggable_mouseUp(event) {
    if (!multiDraggable_arrayState["mouseMoved"]) {
        if (multiDraggable_arrayState["nodeWhereMouseDownWasSelected"]) {
            multiDraggable_arraySelectedNode[multiDraggable_arrayState["nodeWhereMouseDown"].attr("id")] = undefined;
            multiDraggable_arrayState["nodeWhereMouseDown"].css("border-color", "white");
        }
    }

    multiDraggable_arrayState["mousePressed"] = false;
}


function multiDraggable_mouseMove(event) {
    if (!multiDraggable_arrayState["mousePressed"])
        return;

    var arrayDisp = {};
    arrayDisp["x"] = event.pageX - multiDraggable_arrayState["mouseX"];
    arrayDisp["y"] = event.pageY - multiDraggable_arrayState["mouseY"];

    multiDraggable_moveArrayNode(multiDraggable_arraySelectedNode, arrayDisp);

    multiDraggable_arrayState["mouseX"] = event.pageX;
    multiDraggable_arrayState["mouseY"] = event.pageY;
    multiDraggable_arrayState["mouseMoved"] = true;
}

function multiDraggable_moveArrayNode(arraySelectedNode, arrayDisp) {
    for (var nodeId in arraySelectedNode) {
        if (arraySelectedNode[nodeId] == undefined)
            continue;

        var node = $("#" + nodeId);
        var position = node.position();
        node.css("left", position.left + arrayDisp["x"]);
        node.css("top", position.top + arrayDisp["y"]);
    }

    resizable_refreshAllButtonPosition($(".resizableButton"));
}

function multiDraggable_keyDown(event) {
    multiDraggable_arrayState["controlKeyPressed"] = true;
}

function multiDraggable_keyUp(event) {
    multiDraggable_arrayState["controlKeyPressed"] = false;
}

function resizable_do(jqueryElement, type) {
    resizable_arrayState = {};

    resizable_createResizeButton(jqueryElement, type);

    $(document).mouseup(resizable_mouseup);
    $(document).mousemove(resizable_mousemove);
}

function resizable_createResizeButton(jqueryElement, type) {
    jqueryElement.each(function() {
        var elementId = $(this).attr("id") + "_resizableButton";

        $(this).parent().append('<div id="' + elementId + '" class="resizableButton" style="width:7px; height:7px; position:absolute; background-color:red"><div></div></div>');
        $("#" + elementId).css("right", $(this).css("right"));
        $("#" + elementId).css("bottom", $(this).css("bottom"));

        if (type == "horizontal") {
            $("#" + elementId).mousedown(resizable_mousedownHorizontal);
            $("#" + elementId).css("cursor", "e-resize");
        }
        else if (type == "vertical") {
            $("#" + elementId).mousedown(resizable_mousedownVertical);
            $("#" + elementId).css("cursor", "s-resize");
        }
        else {
            $("#" + elementId).mousedown(resizable_mousedownBoth);
            $("#" + elementId).css("cursor", "se-resize");
        }
    });
}

function resizable_mousedownHorizontal(event) {
    resizable_arrayState["resizableType"] = "horizontal";
    resizable_mousedown(event, $(this));
}

function resizable_mousedownVertical(event) {
    resizable_arrayState["resizableType"] = "vertical";
    resizable_mousedown(event, $(this));
}

function resizable_mousedownBoth(event) {
    resizable_arrayState["resizableType"] = "both";
    resizable_mousedown(event, $(this));
}

function resizable_mousedown(event, self) {
    resizable_arrayState["mousePressed"] = true;
    resizable_arrayState["mouseX"] = event.pageX;
    resizable_arrayState["mouseY"] = event.pageY;
    resizable_arrayState["elementWhereMouseDownResizeButton"] = self;
    resizable_arrayState["elementWhereMouseDown"] = $("#" + self.attr("id").split("_")[0]);
}

function resizable_mousemove(event) {
    if (!resizable_arrayState["mousePressed"])
        return;

    var arrayDisp = {};
    arrayDisp["x"] = event.pageX - resizable_arrayState["mouseX"];
    arrayDisp["y"] = event.pageY - resizable_arrayState["mouseY"];

    if (resizable_arrayState["resizableType"] == "horizontal" || resizable_arrayState["resizableType"] == "both")
        resizable_arrayState["elementWhereMouseDown"].css("width", resizable_arrayState["elementWhereMouseDown"].width() + arrayDisp["x"]);

    if (resizable_arrayState["resizableType"] == "vertical" || resizable_arrayState["resizableType"] == "both")
        resizable_arrayState["elementWhereMouseDown"].css("height", resizable_arrayState["elementWhereMouseDown"].height() + arrayDisp["y"]);

    resizable_arrayState["elementWhereMouseDownResizeButton"].css("right", resizable_arrayState["elementWhereMouseDown"].css("right"));
    resizable_arrayState["elementWhereMouseDownResizeButton"].css("bottom", resizable_arrayState["elementWhereMouseDown"].css("bottom"));

    resizable_arrayState["mouseX"] = event.pageX;
    resizable_arrayState["mouseY"] = event.pageY;
}

function resizable_mouseup(event) {
    resizable_arrayState["mousePressed"] = false;
}

function resizable_refreshAllButtonPosition(jqueryElement) {
    jqueryElement.each(function() {
        var buttonId = $(this).attr("id");
        var divId = buttonId.split("_")[0];
        var div = $("#" + divId);
        $(this).css("right", div.css("right"));
        $(this).css("bottom", div.css("bottom"));
    });
}